package com.xueyu.controller;

import com.xueyu.dao.financeDao;
import com.xueyu.dao.personinfoDao;
import com.xueyu.domain.finance.financeBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping("/open-finance")
public class financeController {

    @Autowired
    financeDao financeDao;
    @Autowired
    personinfoDao personinfoDao;

    @RequestMapping(value = "/Add-finance")
    @ResponseBody
    public Map Add_finance(@RequestBody financeBean financeBean){
        Map map = new HashMap();

        return map;
    }

    @RequestMapping(value = "/pay_the_fees-finance")
    @ResponseBody
    public Map pay_the_fees_finance(financeBean financeBean){
        Map map = new HashMap();
        int personId = financeBean.getPersonId();
        List<financeBean> get_financeList = financeDao.select_Max_financeId_where_personId(personId);
        get_financeList.stream()
                .filter(Objects::nonNull)
                .forEach(element ->{
                    financeBean.setStayMoney(element.getStayMoney());
                    financeBean.setPaidType(element.getPaidType());
                    financeBean.setFeeProjId(element.getFeeProjId());
                    financeBean.setModeofpayment(element.getModeofpayment());
                    financeBean.setOperator(element.getOperator());
                    financeBean.setFinaceremark(element.getFinaceremark());
                });
        personinfoManageController personinfoManageController = new personinfoManageController();
        String modeofpayment = financeBean.getModeofpayment();
            String paidDate = financeBean.getPaidDate();
        if ("按日收费".equals(modeofpayment)){
            Map getDay = personinfoManageController.calculate_expiration_time(1,paidDate);
            String expireDate = (String) getDay.get("expireDate");
            int daysToNextMonth = (int) getDay.get("daysToNextMonth");
            financeBean.setExpireDate(expireDate);
        }
        if ("按月收费".equals(modeofpayment)) {
            Map getDay = personinfoManageController.calculate_expiration_time(30,paidDate);
            String expireDate = (String) getDay.get("expireDate");
            int daysToNextMonth = (int) getDay.get("daysToNextMonth");
            financeBean.setExpireDate(expireDate);
        }
        if ("按年收费".equals(modeofpayment)){
            Map getDay = personinfoManageController.calculate_expiration_time(365,paidDate);
            String expireDate = (String) getDay.get("expireDate");
            int daysToNextMonth = (int) getDay.get("daysToNextMonth");
            financeBean.setExpireDate(expireDate);
        }
        int result = financeDao.insert(financeBean);
        if (result != 0){
            personinfoDao.update_personify_state_1(personId);
            map.put("code", 200);
            System.out.println("true row" + result);
        }else {
            System.out.println("Error");
        }
        return map;
    }

}
